package com.relax.condition;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class ConditionDemo {

    Lock lock = new ReentrantLock();
    Condition a = lock.newCondition();
    Condition b = lock.newCondition();
    Condition c = lock.newCondition();

    private int num = 0;

    public void a() {
        lock.lock();
          try {
              System.out.println("a wait before........");
              a.await();
              System.out.println("a wait after........");
          }catch (Exception e){
              e.printStackTrace();
          }finally {
              System.out.println("a 释放锁.......");
              lock.unlock();

          }
//        notifyAll();
    }

    public void b() {
        lock.lock();
        System.out.println("b  signal before........");
        a.signal();
        System.out.println("b  signal after........");

        lock.unlock();

    }

    public void c() {
        lock.lock();
        while (num != 2) {
            try {
                c.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("c........");

        num = 0;
        a.signal();
//        notifyAll();
        lock.unlock();

    }


    public static void main(String[] args) {
        final ConditionDemo demo = new ConditionDemo();

        new Thread(new Runnable() {
            public void run() {
                demo.a();
            }
        }).start();

        new Thread(new Runnable() {
            public void run() {
                try {
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                demo.b();
            }
        }).start();


    }
}
